package com.xing.game.bean;

import com.xing.game.bean.api.BeanProcessor;
import com.xing.game.bean.api.BeanProcessorChain;
import com.xing.game.bean.exception.BeanProcessException;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public abstract class AbstractBeanProcessorChain implements BeanProcessorChain {

	protected List<BeanProcessor> beanProcessors = new CopyOnWriteArrayList<BeanProcessor >();
	private int index = -1;

	public abstract boolean isMatch(Class<?> beanClass);

	public <T> T doProcess(T obj) throws BeanProcessException {
		index++;
		
		if(index < beanProcessors.size()){
			return beanProcessors.get(index).process(obj, this);
		}
		return obj;
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}
